/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.common.util.packagemanager;

import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

public class TarPackageManagerTest extends TestCase {

	protected void setUp() throws Exception {
		
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	/**
	 * @throws IOException 
	 * 
	 */
	public void testUnpackTarGzFile() throws IOException {
		//-- scenario
		String fs = File.separator;
		String fileToUnpackName = "test" + fs + "deployer" + fs + 
			"packageManager" + fs + "teste.tar.gz";
		String folderName = "test" + fs + "deployer" + fs + 
			"packageManager" + fs + "teste1";
		String folderNameRef = "test" + fs + "deployer" + fs + 
			"packageManager" + fs + "testeRef";
		
		File fileToUnpack = new File( fileToUnpackName ).getAbsoluteFile();
		File destinyDirOfUnpack = new File( folderName );
		File destinyDirOfUnpack2 = new File( folderNameRef );

		assertTrue(fileToUnpack.exists());

		PackageManager pm = new PackageManagerImpl();
		pm.unpackIt(destinyDirOfUnpack.getAbsolutePath(), fileToUnpack);

		//-- verifying
		PackageManagerTestUtil pmtu = new PackageManagerTestUtil();
		pmtu.compareFiles(destinyDirOfUnpack, destinyDirOfUnpack2);

	}

	/**
	 * @throws IOException 
	 * 
	 */
	public void testUnpackAndPackTarGzFile() throws IOException {
		//-- scenario
		String fs = File.separator;
		String fileToUnpackName = "test" + fs + "deployer" + fs + 
			"packageManager" + fs + "teste.tar.gz";
		String destinyTarName = "test" + fs + "deployer" + fs + 
			"packageManager" + fs + "teste2.tar.gz";
		String folderName = "test" + fs + "deployer" + fs + 
			"packageManager" + fs + "teste1";
		String folderName2 = "test" + fs + "deployer" + fs + 
			"packageManager" + fs + "teste2";
		
		File fileToUnpack = new File( fileToUnpackName ).getAbsoluteFile();
		File destinyZipFile = new File( destinyTarName );
		File destinyDirOfUnpack = new File( folderName );
		File destinyDirOfUnpack2 = new File( folderName2 );

		assertTrue(fileToUnpack.exists());

		PackageManager pm = new PackageManagerImpl();
		pm.unpackIt(destinyDirOfUnpack.getAbsolutePath(), fileToUnpack);
		pm.packIt( destinyTarName, PackTypes.ZIP, "test" + fs + "deployer" + fs + 
				"packageManager" + fs + "teste1" + File.separator + "JOILSON");
		pm.unpackIt(destinyDirOfUnpack2.getAbsolutePath(), destinyZipFile);

		//-- verifying
		PackageManagerTestUtil pmtu = new PackageManagerTestUtil();
		pmtu.compareFiles(destinyDirOfUnpack, destinyDirOfUnpack2);

	}
}
